package cn.ncu.service;

import cn.ncu.dao.ArticleDao;
import cn.ncu.entity.Article;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleDao articleDao;

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<Article> findByPage(String authorid, Integer currentPage, Integer rows) {
        currentPage = currentPage == null ? 1 : currentPage;
        rows = rows == null ? 4 : rows;
        Integer start = (currentPage - 1) * rows;
        return articleDao.findByPage(authorid, start, rows);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public Integer findTotal(String authorid) {
        return articleDao.findTotal(authorid);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public Article findArticleByArticleid(String articleid) {
        return articleDao.findArticleByArticleid(articleid);
    }

    @Override
    public void saveArticle(Article article) {
        Date date = new Date();
        article.setCreatedtime(date).setModifytime(date);
        articleDao.saveArticle(article);
    }

    @Override
    public void updateArticle(Article article) {
        articleDao.updateArticle(article);
    }

    @Override
    public void deleteArticle(String articleid) {
        articleDao.deleteArticle(articleid);
    }
}
